package com.hnevc.registerandlogin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class RegistActivity extends AppCompatActivity {
    EditText mEtUsername;
    EditText mEtPassword;
    String username;
    String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        mEtUsername = (EditText) findViewById(R.id.id_et_username);
        mEtPassword = (EditText) findViewById(R.id.id_et_password);
    }

    public void registUser(View view) {
        //获取用户输入的用户名
        username = mEtUsername.getText().toString();
        //获取用户输入的密码
        password = mEtPassword.getText().toString();
        //将用户名和密码保存到本地文件
        saveToLocalFile(username, password);
        //回传到登录页面
        Intent intent = new Intent();
        intent.putExtra("username", username);
        setResult(1, intent);
        finish();
    }


    private void saveToLocalFile(String username, String password) {
        //TODO 实现将用户名和密码保存到内部私有文件中
        try {
            //打开内部私有存储文件data.txt
            FileOutputStream fos = openFileOutput("data.txt",MODE_PRIVATE);
            String s = username + "-" + password;
            //将用户名和密码写入data.txt中
            fos.write(s.getBytes());
            //关闭文件
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
